<?php


namespace app\index\controller;


use app\common\controller\IndexBase;
use app\common\model\Share;
use app\common\model\Shareappreciate;
use app\common\model\Sharecomment;

class Life extends IndexBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    public function index(){
        $type = input('type',1);
        $this->assign('type',$type);
        return $this->fetch();
    }

    public function get_list(){
        $type = input('type',1);
        if($type!=1){
            $where['user_id'] =session('user_info')['userid'];
        }
        $where['is_show']=1;
        $title = input('title');
        $title?$where['title'] = ['like','%'.$title.'%']:'';
        $page = input('page');
        $m = new Share();
        $list = $m->where($where)->order('add_time desc')->page($page, 5)->select();
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function insert(){
        if($this->request->isAjax()){
            $data =input();
            $data['add_time']=time();
            $data['user_id'] = session('user_info')['userid'];
            $data['user_name'] =session('user_info')['name'];
            $data['user_head'] = session('user_info')['avatar'];
            $m = new Share();
            if($m->insert($data)!==false){
                return ajax_return(0,'发布成功','life/index');
            }else{
                return ajax_return(1,'发布失败');
            }
        }
       return $this->fetch();
    }

    public function share_detail(){
        $id = input('id');
        $rows = model('share')->find($id);
        $share_apprecitae = new Shareappreciate();
        $appreciate = $share_apprecitae->where(['share_id'=>$id,'status'=>1])->find();
        $this->assign('appreciate',$appreciate);
        $this->assign('rows',$rows);
        return $this->fetch();
    }

    public function appreciate(){
        if($this->request->isAjax()){
            $share_id = input('share_id');
            $do_appreciate = input('do_appreciate');
            $share_appreciate = new Shareappreciate();
            $info = $share_appreciate->where(['user_id'=>session('user_info')['userid']])->find();
            if($do_appreciate==1){
                if($info){
                    $isupdate =1;
                    $data['id'] = $info['id'];
                }else{
                    $isupdate=false;
                }
                $data['user_id'] = session('user_info')['userid'];
                $data['share_id'] = $share_id;
                $data['status'] =1;
                $res = $share_appreciate->isUpdate($isupdate)->save($data);
                if($res!==false){
                    $share = new Share();
                    $share->where(['id'=>$share_id])->setInc('appreciate_number');
                }
            }else{
                $info['status'] =0;
                $info=$info->toArray();
                $res = $share_appreciate->save($info,$info['id']);
                if($res!==false){
                    $share = new Share();
                    $share->where(['id'=>$share_id])->setDec('appreciate_number');
                }
            }
            return ['code'=>0,'msg'=>'操作成功'];
        }
    }

    public function publish_comment(){
        if($this->request->isAjax()){
            $data['content'] = input('content');
            $data['share_id'] = input('share_id');
            $data['user_id'] = session('user_info')['userid'];
            $data['user_name'] =session('user_info')['name'];
            $data['user_head'] =session('user_info')['avatar'];
            $data['add_time'] = time();
            $share_comment = new Sharecomment();
            if(($share_comment->save($data))!==false){
                $share = new Share();
                $share->where(['id'=>$data['share_id']])->setInc('comment_number');
                return ['code'=>1,'msg'=>'操作成功'];
            }else{
                return ['code'=>0,'msg'=>'评论失败'];
            }
         }
    }

    public function get_comment_list(){
        $share_id = input('share_id');
        $page = input('page',1);
        $share_comment = new Sharecomment();
        $list = $share_comment->where(['share_id'=>$share_id])->page($page,10)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }


    public function unshow(){
        $id = input('id');
        if(model('share')->isUpdate(true)->save(['is_show'=>0],['id'=>$id])!==false){
            return ajax_return(1,'撤回成功');
        }else{
            return ajax_return(0,'撤回失败，请联系管理员');
        }
    }
}